#!/usr/bin/python3
# ******************************************************************************
# Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
# licensed under the Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#     http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
# PURPOSE.
# See the Mulan PSL v2 for more details.
# ******************************************************************************/
import csv
import os

from packageship_panel.application.core import ExportBase
from packageship_panel.application.query.panel import PanelInfo


class SigInfo(ExportBase):
    def __init__(self):
        self._panel_info = PanelInfo()

    def sig_infos(self, sig_name=None):
        """获取所有的sig组信息

        Args:
            sig_name (_type_, optional): _description_. Defaults to None.

        Returns:
            _type_: _description_
        """
        return self._panel_info.query_sig_info(sig_name=sig_name, index="sig_info")

    def sig_infos_csv(self):
        """_summary_

        Returns:
            _type_: _description_
        """
        self.create_folder_path()
        sig_path = os.path.join(self.folder_path, "sig.csv")
        sig_infos = self.sig_infos()
        with open(sig_path, "a+", newline="", encoding="utf-8") as sig_csv:
            sig_csv_writer = csv.writer(sig_csv)
            sig_csv_writer.writerow(["sig名称", "描述", "maintainer", "contributors", "所属软件包"])
            for sig_info in sig_infos:
                sig_csv_writer.writerow(
                    [
                        sig_info.get("name", ""),
                        sig_info.get("description", ""),
                        self.parse_owners_maintainers(sig_info.get("maintainer", [])),
                        self.parse_owners_maintainers(sig_info.get("contributors", [])),
                        "https://pkgmanagedebug.openeuler.org/sig-detail?sig_name={}".format(sig_info.get("name")),
                    ]
                )
        return self.folder_path
